
<template>
  <div>
    <el-dialog
      title="智能灯列表添加页面"
      :visible.sync="dialogVisible"
      width="70%"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col span="8">
            <el-form-item
              label="设备类型："
              :label-width="formLabelWidth"
              prop="typeId"
            >
              <el-input v-model="ruleForm.typeId" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item
              label="灯状态："
              :label-width="formLabelWidth"
              prop="lightState"
            >
              <el-switch
                v-model="ruleForm.lightState"
                active-color="#13ce66"
                inactive-color="#ff4949"
                style="width: 1000px"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="8">
            <el-form-item
              label="雷达感应："
              :label-width="formLabelWidth"
              prop="radar"
            >
              <el-switch
                v-model="ruleForm.radar"
                active-color="#13ce66"
                inactive-color="#ff4949"
                style="width: 1000px"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item
              label="报警："
              :label-width="formLabelWidth"
              prop="alram"
            >
              <el-switch
                v-model="ruleForm.alram"
                active-color="#13ce66"
                inactive-color="#ff4949"
                style="width: 1000px"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="8">
            <el-form-item
              label="遥控："
              :label-width="formLabelWidth"
              prop="remoteControl"
            >
              <el-switch
                v-model="ruleForm.remoteControl"
                active-color="#13ce66"
                inactive-color="#ff4949"
                style="width: 1000px"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item
              label="设备温度："
              :label-width="formLabelWidth"
              prop="setTemp"
            >
              <el-input
                v-model="ruleForm.setTemp"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="8">
            <el-form-item
              label="创建时间："
              :label-width="formLabelWidth"
              prop="createDate"
            >
              <el-date-picker
                v-model="ruleForm.createDate"
                align="right"
                type="date"
                placeholder="选择日期"
                :picker-options="pickerOptions"
                style="width: 335px"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item
              label="设备型号："
              :label-width="formLabelWidth"
              prop="unitType"
            >
              <el-input
                v-model="ruleForm.unitType"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')"
        >确 定</el-button
      >
    </el-dialog>
    <el-dialog
      title="智能灯列表修改页面"
      :visible.sync="dialogVisibleupd"
      width="70%"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForms"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col span="8">
            <el-form-item
              label="设备类型："
              :label-width="formLabelWidth"
              prop="typeId"
            >
              <el-input
                v-model="ruleForms.typeId"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item
              label="灯状态："
              :label-width="formLabelWidth"
              prop="lightState"
            >
              <el-switch
                v-model="ruleForms.lightState"
                active-color="#13ce66"
                inactive-color="#ff4949"
                style="width: 1000px"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="8">
            <el-form-item
              label="雷达感应："
              :label-width="formLabelWidth"
              prop="radar"
            >
              <el-switch
                v-model="ruleForms.radar"
                active-color="#13ce66"
                inactive-color="#ff4949"
                style="width: 1000px"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item
              label="报警："
              :label-width="formLabelWidth"
              prop="alram"
            >
              <el-switch
                v-model="ruleForms.alram"
                active-color="#13ce66"
                inactive-color="#ff4949"
                style="width: 1000px"
              >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="8">
            <el-form-item
              label="遥控："
              :label-width="formLabelWidth"
              prop="remoteControl"
            >
              <el-switch
                v-model="ruleForms.remoteControl"
                active-color="#13ce66"
                inactive-color="#ff4949"
                style="width: 1000px"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item
              label="设备温度："
              :label-width="formLabelWidth"
              prop="setTemp"
            >
              <el-input
                v-model="ruleForms.setTemp"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="8">
            <el-form-item
              label="创建时间："
              :label-width="formLabelWidth"
              prop="createDate"
            >
              <el-date-picker
                v-model="ruleForms.createDate"
                align="right"
                type="date"
                placeholder="选择日期"
                :picker-options="pickerOptions"
                style="width: 335px"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col span="8">
            <el-form-item
              label="设备型号："
              :label-width="formLabelWidth"
              prop="unitType"
            >
              <el-input
                v-model="ruleForms.unitType"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-button @click="dialogVisibleupd = false">取 消</el-button>
      <el-button type="primary" @click="updatedatas">修 改</el-button>
    </el-dialog>
    <el-dialog
      title="智能灯列表查看页面"
      :visible.sync="dialogVisibleupdate"
      width="20%"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForms"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="设备类型：" :label-width="formLabelWidth">
          {{ ruleForms.typeId }}
        </el-form-item>
        <el-form-item label="灯状态：" :label-width="formLabelWidth">
          {{ ruleForms.lightState == true ? "开" : "关" }}
        </el-form-item>
        <el-form-item label="雷达感应：" :label-width="formLabelWidth">
          {{ ruleForms.radar == true ? "开" : "关" }}
        </el-form-item>
        <el-form-item label="报警：" :label-width="formLabelWidth">
          {{ ruleForms.alram == true ? "开" : "关" }}
        </el-form-item>
        <el-form-item label="遥控：" :label-width="formLabelWidth">
          {{ ruleForms.remoteControl == true ? "开" : "关" }}
        </el-form-item>
        <el-form-item label="设备温度：" :label-width="formLabelWidth">
          {{ ruleForms.setTemp }}
        </el-form-item>
        <el-form-item label="创建时间：" :label-width="formLabelWidth">
          {{ ruleForms.createDate }}
        </el-form-item>
        <el-form-item label="设备型号：" :label-width="formLabelWidth">
          {{ ruleForms.unitType }}
        </el-form-item>
      </el-form>
    </el-dialog>
    <template>
      <el-form
        :inline="true"
        class="demo-form-inline"
        style="text-align: left"
        :model="ruleFormData"
      >
        <el-form-item label="设备类型：">
          <el-input
            v-model="ruleFormData.typeId"
            prop="typeIds"
            placeholder="输入设备编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="设备型号：">
          <el-input
            v-model="ruleFormData.unitType"
            prop="unitTypes"
            placeholder="输入设备型号"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="创建时间：">
          <el-input v-model="ruleFormData.createDate" placeholder="输入创建时间"></el-input>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="searchData">查询</el-button>
          <el-button type="info" @click="resetForm('ruleFormData')"
            >重置</el-button
          >
          <el-button type="primary" @click="dialogVisible = true"
            >添加</el-button
          >
          <el-button type="danger" @click="deletedata">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="序号" width="180"> </el-table-column>
        <el-table-column prop="typeId" label="设备类型" width="180">
        </el-table-column>
        <el-table-column prop="lightState" label="灯状态" width="180">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.lightState"
              active-color="#13ce66"
              inactive-color="#ff4949"
              disabled
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="radar" label="雷达感应" width="180">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.radar"
              active-color="#13ce66"
              inactive-color="#ff4949"
              disabled
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="alram" label="报警" width="180">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.alram"
              active-color="#13ce66"
              inactive-color="#ff4949"
              disabled
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="remoteControl" label="遥控" width="180">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.remoteControl"
              active-color="#13ce66"
              inactive-color="#ff4949"
              disabled
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="setTemp" label="设备温度" width="180">
          <template slot-scope="scope">
            {{ fun(scope.row.setTemp) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="createDate"
          label="创建时间"
          width="180"
          :formatter="formatDate"
        >
        </el-table-column>
        <el-table-column prop="unitType" label="设备型号" width="180">
        </el-table-column>
        <el-table-column prop="" label="操作" width="180">
          <el-link type="primary" @click="see">状态</el-link>
          <el-link type="primary" @click="updatedata">修改</el-link>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="ruleFormData.pageIndex"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="ruleFormData.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="count"
    >
    </el-pagination>
  </div>
</template>

<script>
import {
  getSmartLampData,
  addSmartLampData,
  deleteSmartLampData,
  updateSmartLampData,
  deleteSmartLampDatas,
  GetCount,
  GetSmartLampsPage,
} from "@/api/iotInfo/SmartLamps";

export default {
  data() {
    return {
      ruleForm: {
        // typeId: null,
        lightState: true,
        radar: true,
        alram: true,
        remoteControl: true,
        // setTemp: null,
        // creaetDate: null,
        // unitType: null,
      },
      ruleForms: {},
      ruleFormData: {
        pageIndex: 1,
        pageSize: 6,
        typeId: "",
        unitType: "",
      },
      count: 0,
      countnum: 0,
      //规则验证
      rules: {
        typeId: [
          { required: true, message: "设备类型不能为空", trigger: "blur" },
          { max: 30, message: "最大长度为30字符", trigger: "blur" },
        ],
        // lightState: [{ required: true, message: '灯状态不能为空', trigger: 'blur' }],
        // radar: [{ required: true, message: '雷达感应不能为空', trigger: 'blur' }],
        // alram: [{ required: true, message: '报警不能为空', trigger: 'blur' }],
        // remoteControl: [{ required: true, message: '遥控不能为空', trigger: 'blur' }],
        setTemp: [
          { required: true, message: "设备温度不能为空", trigger: "blur" },
        ],
        createDate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        unitType: [
          { required: true, message: "设备型号不能为空", trigger: "blur" },
          { max: 30, message: "最大长度为30字符", trigger: "blur" },
        ],
      },
      //默认关闭弹出框
      dialogVisible: false,
      dialogVisibleupd: false,
      dialogVisibleupdate: false,
      //显示的数据
      tableData: [],
      multipleSelection: [],
      ids: [],
    };
  },

  created() {
    this.ListData();
    this.totalcount();
  },
  methods: {
    //获取总条数方法
    totalcount() {
      GetCount().then((res) => {
        this.countnum = res.data;
      });
    },
    //弹出框关闭
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    totalcount() {
      GetTotaocount().then((res) => {
        this.countnum = res.data;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.ruleFormData.pageSize = val;
      this.ListData();
    },
    handleCurrentChange(val) {
      this.ruleFormData.pageIndex = val;
      this.ListData();
    },
    //显示方法
    ListData() {
      GetSmartLampsPage(this.ruleFormData).then((res) => {
        this.tableData = res.data.data;
        this.count = res.data.totalCount;
      });
    },
    //查询验证
    searchData() {
      if (this.ruleFormData.typeId == "" && this.ruleFormData.unitType == "") {
        this.$message({
          message: "请输入查询数据",
          type: "error",
        });
        return;
      }
      this.ListData();
    },
    //添加方法
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addSmartLampData(this.ruleForm).then((res) => {
            if (res.data) {
              this.$message({
                message: "添加成功",
                type: "success",
              });
              this.dialogVisible = false;
              this.ListData();
            } else {
              this.$message({
                message: "添加失败，数据格式可能有误",
                type: "error",
              });
            }
            return;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //批量删除方法
    deletedata() {
      if (this.multipleSelection.length <= 0) {
        this.$message.error("请选择一条数据");
        return;
      }
      this.multipleSelection.forEach((res) => {
        this.ids.push(res.id);
      });
      deleteSmartLampDatas(this.ids).then((res) => {
        if (res.data) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.ListData();
        } else {
          this.$message({
            message: "删除失败",
            type: "error",
          });
          return;
        }
      });
    },
    //反填方法
    updatedata() {
      if (this.multipleSelection.length < 1) {
        this.$message.error("请选择数据");
        return;
      }
      if (this.multipleSelection.length > 1) {
        this.$message.error("不允许选择多条数据");
        return;
      }
      this.dialogVisibleupd = true;
      this.multipleSelection.forEach((res) => {
        this.ruleForms = res;
      });
    },
    //修改方法
    updatedatas() {
      updateSmartLampData(this.ruleForms).then((res) => {
        if (res.data) {
          this.$message.success("修改成功");
          this.dialogVisibleupd = false;
          this.ListData();
        } else {
          this.$message.success("修改失败");
        }
      });
    },
    //显示两位小数
    fun(val) {
      return Number(val).toFixed(2);
    },
    //表格显示具体时间
    formatDate(row, column) {
      // 获取单元格数据
      let data = row[column.property];
      if (data == null) {
        return 1;
      }
      let dt = new Date(data);
      return (
        dt.getFullYear() +
        "-" +
        (dt.getMonth() + 1) +
        "-" +
        dt.getDate() +
        " " +
        dt.getHours() +
        ":" +
        dt.getMinutes() +
        ":" +
        dt.getSeconds()
      );
    },
    //重置方法
    resetForm() {
      // this.$refs[formName].resetFields();
      this.ruleFormData.typeId = "";
      this.ruleFormData.unitType = "";
    },
    //查看方法
    see() {
      if (this.multipleSelection.length < 1) {
        this.$message.error("请选择数据");
        return;
      }
      if (this.multipleSelection.length > 1) {
        this.$message.error("不允许选择多条数据");
        return;
      }
      this.dialogVisibleupdate = true;
      this.multipleSelection.forEach((res) => {
        this.ruleForms = res;
      });
    },
  },
};
</script>

